<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/26 0026
 * Time: 下午 4:09
 * Author: NanTao <260053530@qq.com>
 */

namespace app\bjwlmanager\controller;
use think\Db;
class Coupon extends Common
{
    public function index()
    {
        $list = Db::name('coupon')->paginate(15,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function add()
    {
        if (request()->isPost())
        {
            $data = input('param.');
            $result = Db::name('coupon')->insert($data);
            if($result)
            {
                json_msg(0,'添加失败');
            } else {
                json_msg(1,'添加成功');
            }
        } else {
            return $this->fetch();
        }
    }
    public function edit()
    {
        $id = input('param.id');
        if (request()->isPost())
        {
            $data = input('param.');
            $result = Db::name('coupon')->where('id',$id)->update($data);
            if($result)
            {
                json_msg(0,'编辑失败');
            } else {
                json_msg(1,'编辑成功');
            }
        } else {
            $info = Db::name('coupon')->find($id);
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
    //删除
    public function del()
    {
        $id = input('post.id');
        if(!isset($id))
        {
            json_msg(1,'请求参数错误');
        }
        $result = Db::name('coupon')->where('id',$id)->delete();
        if($result==0)
        {
            json_msg(1,'删除失败');
        } else {
            json_msg(0,'删除成功');
        }
    }

    //停用
    public function stop()
    {
        $id = input('post.id');
        if(!isset($id))
        {
            json_msg(1,'请求参数错误');
        }
        $result = Db::name('coupon')->where('id',$id)->setField('status',1);
        if($result==0)
        {
            json_msg(0,'状态未更改');
        } else {
            json_msg(0,'禁用成功');
        }
    }

    //启用
    public function start()
    {
        $id = input('post.id');
        if(!isset($id))
        {
            json_msg(1,'请求参数错误');
        }
        $result = Db::name('coupon')->where('id',$id)->setField('status',0);
        if($result == 0)
        {
            json_msg(0,'状态未更改');
        } else {
            json_msg(0,'启用成功');
        }
    }
}